<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input
          v-model="dataForm.username"
          placeholder="手机号/账号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.campusId" placeholder="请选择校区">
          <el-option
            v-for="campus in campusList"
            :key="campus.campusId"
            :label="campus.campusName"
            :value="campus.campusId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.userType" placeholder="请选择用户类型">
          <el-option
            label="体验账号"
            :value="0"
          >
          </el-option>
          <el-option
            label="激活账号"
            :value="1"
          >
          </el-option>
          <el-option
            label="生成账号"
            :value="2"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      stripe
      v-loading="dataListLoading"
      style="width: 100%"
    >
      <el-table-column prop="id" label="用户id" width="180"> </el-table-column>
      <el-table-column prop="mobile" label="手机号/账号" width="180">
      </el-table-column>
      <el-table-column prop="campusName" label="所属校区名称">
      </el-table-column>
      <el-table-column prop="userType" label="用户类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.userType == 0">体验账号</el-tag>
          <el-tag v-else-if="scope.row.userType == 1">激活账号</el-tag>
          <el-tag v-else>生成账号</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间"> </el-table-column>
      <el-table-column prop="stoptime" label="到期时间"> </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="updateHandle(scope.row.id)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <update
      v-if="addOrUpdateVisible"
      ref="update"
    ></update>
  </div>
  
</template>

<script>
import Update from "./userroleauthorize-update";
export default {
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataForm: {
        username: "",
        campusId: null,
        userType: null,
      },
      userId: null,
      macStatus: null,
      campusList: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    Update,
  },
  activated() {
    this.getDataList();
    this.getCampusList();
  },
  methods: {
    // 获取用户分页数据
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sf/frontuser/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          username: this.dataForm.username,
          campusId: this.dataForm.campusId,
          userType: this.dataForm.userType,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.userPage.records;
          this.totalPage = data.userPage.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 获取所有校区
    getCampusList() {
      this.$http({
        url: this.$http.adornUrl("/sf/campus/getCampusList"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data);
          this.campusList = data.campusList;
        } else {
          this.campusList = [];
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 修改
    updateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.update.init(id);
      });
    },
  },
};
</script>